<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Service\GfunService;
use Hyperf\Context\ApplicationContext;
use Hyperf\Coroutine\Coroutine;
use App\Task\TaskHandle;
use Hyperf\DbConnection\Db;

class IndexController extends AbstractController
{
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();
        $card = Db::table('xc_iccard')->where('number','=','1034033')->first();
        if($card){
            $money = Db::table('xc_money')->where('user_id','=',$card['uid'])->value('money');
        }
        var_dump($card);
        return [
            'method' => $method,
            'message' => "11Hello {$user}.",
        ];
    }

    //04充电关闭上报
    //空载5AA51100542904000800020001000800002FF67887

//     #[Inject]
//     private GfunService $gfunService;
    public function test()
    {
        $macNo = $this->request->input('station_id');
        $method = $this->request->getMethod();
        $redis = redis();

        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA511005429540002000100007887"));
        return 'OK';
    }
    //
    public function test53()
    {
        $macNo = $this->request->input('station_id');
        $method = $this->request->getMethod();
        $redis = redis();

        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA5110054295300060102160000009BC37887"));
        return 'OK';
    }

    public function test54()
    {
        $macNo = $this->request->input('station_id');
        $method = $this->request->getMethod();
        $redis = redis();

        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA511005429540002000100007887"));
        return 'OK';
    }
    public function test54_1()
    {
        $macNo = $this->request->input('station_id');
        $method = $this->request->getMethod();
        $redis = redis();

        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA511005429540111010207139301020713930200007887"));
        return 'OK';
    }


    //5AA5110054295401010090B97887 主机请求
    public function test54_2()
    {
        $macNo = $this->request->input('station_id');
        $method = $this->request->getMethod();
        $redis = redis();

        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA51100542954001101020713930200007887"));
        return 'OK';
    }

    public function test20_1()
    {
        $macNo = $this->request->input('station_id');
        $redis = redis();
        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA5110054292000040001010200007887"));
        return 'OK';
    }

    public function test20_2()
    {
        $macNo = $this->request->input('station_id');
        $redis = redis();
        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA5110054292000040002010200007887"));
        return 'OK';
    }

    public function stop20_1()
    {
        $macNo = $this->request->input('station_id');
        $redis = redis();
        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA5110054292000040001000200007887"));
        //结果5AA511005429200003010100CB2E7887
        return 'OK';
    }

    public function stop20_2()
    {
        $macNo = $this->request->input('station_id');
        $redis = redis();
        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA5110054292000040002000200007887"));
        return 'OK';
    }

    public function test55()
    {
        $macNo = $this->request->input('station_id');
        $redis = redis();
        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA51100542955001100007887"));
        return 'OK';
    }


    public function test40()
    {
        $macNo = $this->request->input('station_id');
        $redis = redis();
        $data = $redis->get($macNo . ":COM");
        if(!$data) return "fail";
        $data = json_decode($data,true);
        var_dump($macNo);
        var_dump($data['port']);
        server()->send($data['port'],hex2bin("5AA511005429400002000100007887"));
        return 'OK';
    }

    public function test_post(){
        $redisMac = [
            "macno" => 1,
            "ip" => 2,
            "port" => 3,
            "lastbeat" => time(),
            "lastbeat_date" => date('Y-m-d H:i:s',time()),
        ];
        $url = \Hyperf\Support\env('CALLBACK_URL');
        self::createtask($url,http_build_query($redisMac));//异步发POST，不阻塞等待接收
    }

    /**
     * 创建异步任务
     */
    static function createtask($url,$data,$type=1,$anotherdata=array()){
        $taskdata['data'] = $data;
        $taskdata['url'] = $url;
        $taskdata['type'] = $type;
        if(count($anotherdata)>0){
            foreach($anotherdata as $k=>$v){
                $taskdata[$k] = $v;
            }
        }
        $container = ApplicationContext::getContainer();
        $task = $container->get(TaskHandle::class);
        $result = $task->handle(Coroutine::id(),$taskdata);

        return 1;
    }

}
